package com.edu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.dto.UserDto;
import com.edu.entity.Menus;
import com.edu.entity.School;
import com.edu.entity.StuInfo;
import com.edu.entity.Users;
import com.edu.service.SchoolService;
import com.edu.service.StuInfoService;
import com.edu.service.UsersService;
import com.edu.vo.Result;
import com.edu.vo.UserVo;
import com.edu.vo.UsersLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: graduate
 * @description: 用户控制层
 * @author: K.faWu
 * @create: 2021-05-22 19:01
 **/

@RestController
@RequestMapping("user")
@Api()  //标识这个类是swagger的资源
public class UsersController {

    @Autowired
    private UsersService usersService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private StuInfoService stuInfoService;

    /**
     * 完善学生信息
     */
    @PostMapping("/upInfo")
    public Result upInfo(@RequestBody UserDto userDto){
        usersService.updateById(userDto.getUser());
        stuInfoService.upStuInfo(userDto.getStuInfo());
        return Result.success();
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/{id}")
    public Result getUserInfo(@PathVariable Long id){
        UserVo userVo = usersService.getUserInfo(id);
        return Result.success(userVo);
    }

    /**
     * 登录接口
     * @param usersLoginVo
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody UsersLoginVo usersLoginVo){
        return usersService.login(usersLoginVo);
    }

    /**
     * 注册接口
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody Users user){
        return usersService.register(user);
    }

    /**
     * 查询权限列表
     * */
    @GetMapping("queryMenuList/{id}")
    public Result<List<Menus>> queryMenuList(@PathVariable Integer id){
        List<Menus> menus = usersService.queryMenuList(id);
        return Result.success(menus);
    }

    /**
     * 查询全部学校
     * @return
     */
    @ApiOperation(notes = "内容提示", value = "方法描述 ")
    @GetMapping("/queryAllSchool")
    public Result<List<School>> querySchoolList(){
        List<School> schoolList = schoolService.list();
        return Result.success(schoolList);
    }

    /**
     * 查询全部学校
     * @return
     */
    @GetMapping("/queryAllSchoolLike/{name}")
    public Result<List<School>> querySchoolList(@PathVariable String name){
        QueryWrapper<School> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            qw.like("name", name);
        }
        List<School> schoolList = schoolService.list(qw);
        return Result.success(schoolList);
    }


}
